const fs = require('fs');
const path = require('path');

class DocsController {
    // 提供API文档页面
    static async getApiDocs(ctx) {
        try {
            const docsPath = path.join(__dirname, '../../api-docs.html');
            const docsContent = fs.readFileSync(docsPath, 'utf8');

            ctx.type = 'text/html';
            ctx.body = docsContent;
        } catch (error) {
            console.error('获取API文档失败:', error);
            ctx.status = 500;
            ctx.body = {
                success: false,
                message: 'API文档加载失败'
            };
        }
    }

    // 提供Swagger UI页面
    static async getSwaggerUI(ctx) {
        try {
            const swaggerPath = path.join(__dirname, '../../swagger-ui.html');
            const swaggerContent = fs.readFileSync(swaggerPath, 'utf8');

            ctx.type = 'text/html';
            ctx.body = swaggerContent;
        } catch (error) {
            console.error('获取Swagger UI失败:', error);
            ctx.status = 500;
            ctx.body = {
                success: false,
                message: 'Swagger UI加载失败'
            };
        }
    }
    static async getApiSpec(ctx) {
        try {
            const apiSpec = {
                openapi: "3.0.0",
                info: {
                    title: "TikTok Spider API",
                    description: "TikTok商品数据爬取与分析系统API",
                    version: "1.0.0"
                },
                servers: [
                    {
                        url: `http://localhost:${process.env.PORT || 3000}`,
                        description: "开发服务器"
                    }
                ],
                tags: [
                    {
                        name: "认证配置",
                        description: "系统认证Token配置管理"
                    },
                    {
                        name: "商品管理",
                        description: "商品信息的增删改查操作"
                    },
                    {
                        name: "爬虫功能",
                        description: "TikTok智能爬虫相关功能"
                    },
                    {
                        name: "Shoplus集成",
                        description: "Shoplus API集成服务"
                    },
                    {
                        name: "视频数据",
                        description: "商品相关视频数据获取"
                    },
                    {
                        name: "完整流程",
                        description: "一键完整处理流程"
                    }
                ],
                paths: {
                    "/api/products": {
                        post: {
                            summary: "处理商品链接",
                            description: "通过TikTok商品链接提取基础商品信息",
                            requestBody: {
                                required: true,
                                content: {
                                    "application/json": {
                                        schema: {
                                            type: "object",
                                            properties: {
                                                url: {
                                                    type: "string",
                                                    description: "TikTok商品页面URL"
                                                }
                                            },
                                            required: ["url"]
                                        }
                                    }
                                }
                            }
                        },
                        get: {
                            summary: "获取商品列表",
                            description: "获取数据库中所有商品的列表信息"
                        }
                    },
                    "/api/products/{id}": {
                        get: {
                            summary: "获取单个商品信息",
                            description: "根据商品ID获取详细的商品信息",
                            parameters: [
                                {
                                    name: "id",
                                    in: "path",
                                    required: true,
                                    schema: {
                                        type: "string"
                                    }
                                }
                            ]
                        }
                    },
                    "/api/config/auth-token": {
                        post: {
                            summary: "设置认证Token",
                            description: "设置系统的认证Token，用于Shoplus API调用"
                        },
                        delete: {
                            summary: "清除认证Token",
                            description: "清除当前设置的认证Token"
                        }
                    }
                }
            };

            ctx.body = apiSpec;
        } catch (error) {
            console.error('获取API规范失败:', error);
            ctx.status = 500;
            ctx.body = {
                success: false,
                message: 'API规范获取失败'
            };
        }
    }
}

module.exports = DocsController;